package com.ruoyi.cases.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.cases.domain.JlStatisticsMouth;
import com.ruoyi.cases.domain.JlTroubleshootingTable;
import com.ruoyi.cases.mapper.JlTroubleshootingTableMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cases.mapper.JlDailyStatisticsMapper;
import com.ruoyi.cases.domain.JlDailyStatistics;
import com.ruoyi.cases.service.IJlDailyStatisticsService;

/**
 * 个人日统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-21
 */
@Service
public class JlDailyStatisticsServiceImpl implements IJlDailyStatisticsService 
{
    @Autowired
    private JlDailyStatisticsMapper jlDailyStatisticsMapper;

    @Autowired
    private JlTroubleshootingTableMapper jlTroubleshootingTableMapper;


    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询个人日统计
     * 
     * @param id 个人日统计主键
     * @return 个人日统计
     */
    @Override
    public JlDailyStatistics selectJlDailyStatisticsById(String id)
    {
        return jlDailyStatisticsMapper.selectJlDailyStatisticsById(id);
    }

    /**
     * 查询个人日统计列表
     * 
     * @param jlDailyStatistics 个人日统计
     * @return 个人日统计
     */
    @Override
    public List<JlDailyStatistics> selectJlDailyStatisticsList(JlDailyStatistics jlDailyStatistics)
    {
        return jlDailyStatisticsMapper.selectJlDailyStatisticsList(jlDailyStatistics);
    }

    /**
     * 新增个人日统计
     * 
     * @param jlDailyStatistics 个人日统计
     * @return 结果
     */
    @Override
    public int insertJlDailyStatistics(JlDailyStatistics jlDailyStatistics)
    {
        return jlDailyStatisticsMapper.insertJlDailyStatistics(jlDailyStatistics);
    }

    /**
     * 修改个人日统计
     * 
     * @param jlDailyStatistics 个人日统计
     * @return 结果
     */
    @Override
    public int updateJlDailyStatistics(JlDailyStatistics jlDailyStatistics)
    {
        /*审核时需不补全关信息*/
        if(StringUtils.isNotNull(jlDailyStatistics.getAuditUserId())){
            jlDailyStatistics.setAssessorName(sysUserMapper.selectUserById(SecurityUtils.getLoginUser().getUserId()).getNickName());
            jlDailyStatistics.setAssessorTime(DateUtils.getDate());
        }
        /*如果当前操作为设置定额标准*/
        if(StringUtils.isNotNull(jlDailyStatistics.getQuotaStandard())){
            BigDecimal bigDecimal = new BigDecimal(jlDailyStatistics.getRatedCoefficient());
            /*计算奖励扣除金额*/
            jlDailyStatistics.setRewardDeductionAmount(((bigDecimal.subtract(new BigDecimal("1"))).multiply(new BigDecimal(jlDailyStatistics.getParam())).multiply(new BigDecimal(jlDailyStatistics.getQuotaStandard()))).setScale(2, RoundingMode.HALF_UP).toString());
        }
        return jlDailyStatisticsMapper.updateJlDailyStatistics(jlDailyStatistics);
    }

    /**
     * 批量删除个人日统计
     * 
     * @param ids 需要删除的个人日统计主键
     * @return 结果
     */
    @Override
    public int deleteJlDailyStatisticsByIds(String[] ids)
    {
        return jlDailyStatisticsMapper.deleteJlDailyStatisticsByIds(ids);
    }

    /**
     * 删除个人日统计信息
     * 
     * @param id 个人日统计主键
     * @return 结果
     */
    @Override
    public int deleteJlDailyStatisticsById(String id)
    {
        return jlDailyStatisticsMapper.deleteJlDailyStatisticsById(id);
    }

    @Override
    public List<JlDailyStatistics> getDetail(JlDailyStatistics jlDailyStatistics) {
        return jlDailyStatisticsMapper.getDetail(jlDailyStatistics);
    }

    @Override
    public List<JlTroubleshootingTable> getMouthStatisticsList(JlTroubleshootingTable jlTroubleshootingTable) {
        List<JlTroubleshootingTable> list = jlTroubleshootingTableMapper.mouthStatistics(jlTroubleshootingTable);
        for(JlTroubleshootingTable j:list){
            if(StringUtils.isNotNull(j.getQuantitativeRatedUnitValue())&&j.getQuantitativeRatedUnitValue().contains(",")){
                    j.setQuantitativeRatedUnitValue(j.getQuantitativeRatedUnitValue().split(",")[0]);
            }
        }
        return list;
    }
}
